package net.kazed.nextaction.project;

import java.text.SimpleDateFormat;
import java.util.Date;

import net.kazed.nextaction.ApplicationResource;
import net.kazed.nextaction.R;
import net.kazed.nextaction.database.Task;
import net.kazed.nextaction.database.TaskStatus;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StrikethroughSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ProjectView extends LinearLayout {

    private static final String tag = "TaskView";

    protected TextView mDescription;
    protected ImageView mIcon;
    protected TextView type;
    protected TextView mDueDate;
    private ApplicationResource applicationResource;

    public ProjectView(Context androidContext, ApplicationResource applicationResource) {
        super(androidContext);
        this.applicationResource = applicationResource;
        
        LayoutInflater vi = (LayoutInflater) androidContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        vi.inflate(R.layout.task_list_item_view, this, true);

        mDescription = (TextView) findViewById(R.id.description);
        mIcon = (ImageView) findViewById(R.id.icon);
        type = (TextView) findViewById(R.id.type);
        mDueDate = (TextView) findViewById(R.id.due_date);
    }

    /**
     * Update view with task data.
     * @param task Task.
     */
    public void updateView(Task task) {
        CharSequence description = task.getDescription();
        if (task.getComplete()) {
            // add strike-through for completed tasks
            SpannableString desc = new SpannableString(description);
            desc.setSpan(new StrikethroughSpan(), 0, description.length(), Spanned.SPAN_PARAGRAPH);
            description = desc;
            mIcon.setVisibility(View.VISIBLE);
        } else {
            mIcon.setVisibility(View.INVISIBLE);
        }
        type.setText(getResources().getString(applicationResource.getTaskTypeAbbreviation(task.getStatus(TaskStatus.UNPROCESSED))));
        type.setBackgroundResource(applicationResource.getTaskTypeBackground(task.getStatus(TaskStatus.UNPROCESSED)));
        mDescription.setText(description);
        if (task.getDueDate() != null) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            mDueDate.setText(dateFormat.format(task.getDueDate()));
            Date date = new Date();
            if (date.before(task.getDueDate())) {
                // task is overdue
                mDueDate.setTypeface(Typeface.DEFAULT_BOLD);
                mDueDate.setTextColor(Color.RED);
            } else {
                mDueDate.setTypeface(Typeface.DEFAULT);
                mDueDate.setTextColor(getContext().getResources().getColor(R.drawable.date_blue));
            }
            mDueDate.setVisibility(View.VISIBLE);
        } else {
            mDueDate.setVisibility(View.GONE);
        }
    }

}
